/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package eypweb_bsc;

import java.sql.*;

/**
 *
 * @author Kohalmi Istvan
 */

// egyszerusitett deviza modell
public class Deviza {
    // megnevezes
    private String name;
    // vetitesi egyseg
    private float unit;
    
    public Deviza(){
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the unit
     */
    public float getUnit() {
        return unit;
    }

    /**
     * @param unit the unit to set
     */
    public void setUnit(float unit) {
        this.unit = unit;
    }
    
    // aktualis deviza megtalalasa adott ertekpapirhoz
    public static Deviza getCurrentDeviza(int isinId)
    {
        Director director = Director.getInstance();
        AccessInfo ai = director.getAccessInfo();
        Deviza d = new Deviza();
        try{
            Connection con = DriverManager.getConnection("jdbc:sqlserver://" + ai.getIPAddress() + ":" + ai.getPort() + ";databaseName=" + ai.getDBName(), ai.getUser(), ai.getPass());
            String query = "SELECT * FROM deviza "
                    + "JOIN isin_deviza ON isin_deviza.id_deviza=deviza.id "
                    + "JOIN isin ON isin_deviza.id_isin = isin.id "
                    + "WHERE isin.id = ? "
                    + "AND ? BETWEEN isin_deviza.hatalytol AND isin_deviza.hatalyig";
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setInt(1, isinId);
            stmt.setDate(2, new Date(new java.util.Date().getTime()));
            ResultSet rs = stmt.executeQuery();
            rs.next();
            d.setName(rs.getNString(2));
            d.setUnit(rs.getFloat(3));
            con.close();
        }
        catch(SQLException sqle)
        {
            System.err.println(sqle.getMessage());
        }
        return d;
    }
    
}
